package com.pap.rbac.roleresource.controller;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.pap.base.controller.BaseController;
import com.pap.base.util.jackson.JacksonUtilss;
import com.pap.obj.vo.response.ResponseVO;
import com.pap.rbac.agent.roleresource.IRoleResourceRelAgent;
import com.pap.rbac.agent.roleresource.dto.RoleResourceRelDTO;
import com.pap.rbac.roleresource.auto.entity.RoleResourceRel;

@RestController
@RequestMapping("/v1/rbac/roleresourcerel")
public class RoleResourceRelController extends BaseController<RoleResourceRel, IRoleResourceRelAgent> {

	private static Logger logger  =  LoggerFactory.getLogger(RoleResourceRelController.class);

	@Resource(name = "roleResourceRelAgent")
	@Override
	public void setService(IRoleResourceRelAgent service) {
		super.service = service;
	}
	
	@RequestMapping(value = "/saverels", method = {RequestMethod.POST})
	public ResponseVO<RoleResourceRel> saveRels(@RequestHeader(value = "clientLicenseId", defaultValue = "-1") String clientLicenseId,
			@RequestHeader(value = "loginUserId", defaultValue = "-1") String loginUserId,
			@RequestParam(value = "saveRelsObj", required = true) String saveRelsObj) throws Exception {
		
		ObjectMapper objectMapper = JacksonUtilss.getObjectMapper();
		RoleResourceRelDTO roleResourceRelDTO = objectMapper.readValue(saveRelsObj, RoleResourceRelDTO.class);
		System.out.println(roleResourceRelDTO.toString());
		service.updateRoleResourceRel(clientLicenseId, roleResourceRelDTO.getRoleId(), roleResourceRelDTO.getResourceIds());
		return ResponseVO.successdata("操作成功");
	}
	
	@RequestMapping(value = "/selectresourceidsbyroleid", method = {RequestMethod.POST})
	public ResponseVO<RoleResourceRel> selectResourceIdsByRoleId(@RequestHeader(value = "clientLicenseId", defaultValue = "-1") String clientLicenseId,
			@RequestHeader(value = "loginUserId", defaultValue = "-1") String loginUserId,
			@RequestParam(value = "roleObj", required = true) String roleObj) throws Exception {
		ObjectMapper objectMapper = JacksonUtilss.getObjectMapper();
		RoleResourceRelDTO roleResourceRelDTO = objectMapper.readValue(roleObj, RoleResourceRelDTO.class);

		List<String> resourceIdsList = service.selectRoleResourceIds(roleResourceRelDTO.getRoleId());
		
		return ResponseVO.successdatas(resourceIdsList, null);
	}
}
